<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="轮播图名称" prop="carouselName">
        <el-input
            v-model="queryParams.carouselName"
            placeholder="请输入轮播图名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="轮播图跳转链接" prop="carouselLink">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.carouselLink"-->
      <!--          placeholder="请输入轮播图跳转链接"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['order:carousel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['order:carousel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['order:carousel:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['order:carousel:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carouselList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="轮播图id，主键自增" align="center" prop="carouselId" />-->
      <el-table-column label="轮播图" align="center" prop="carouselImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.carouselImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="轮播图名称" align="center" prop="carouselName" />
      <el-table-column label="轮播图类型" align="center" prop="carouselType" />
      <el-table-column label="轮播图跳转链接" align="center" prop="carouselLink" />

      <!--      <el-table-column label="状态" align="center" prop="status" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:carousel:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:carousel:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改轮播图对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="carouselRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="轮播图名称" prop="carouselName" label-width="95px">
          <el-input v-model="form.carouselName" placeholder="请输入轮播图名称" style="width: 150px"/>
        </el-form-item>
        <el-form-item label="轮播图跳转链接" prop="carouselLink" label-width="120px">
          <el-input v-model="form.carouselLink" placeholder="请输入轮播图跳转链接" style="width: 200px" />
        </el-form-item>
        <el-form-item label="轮播图图片路径" prop="carouselImage" label-width="120px">
          <image-upload v-model="form.carouselImage"/>
        </el-form-item>
        <el-form-item label="是否为平台轮播图" prop="isPlatform" label-width="140px">
          <el-select v-model="form.isPlatform" placeholder="请选择" style="width: 140px;">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Carousel">
import { listCarousel, getCarousel, delCarousel, addCarousel, updateCarousel } from "@/api/order/carousel"

const { proxy } = getCurrentInstance()

const carouselList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    carouselName: null,
    carouselType: null,
    carouselLink: null,
    carouselImage: null,
    status: null,
    isPlatform : null
  },
  rules: {
    carouselName: [
      { required: true, message: "轮播图名称不能为空", trigger: "blur" }
    ],
    carouselType: [
      { required: true, message: "轮播图类型不能为空", trigger: "change" }
    ],
    carouselLink: [
      { required: true, message: "轮播图跳转链接不能为空", trigger: "blur" }
    ],
    carouselImage: [
      { required: true, message: "轮播图图片路径不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "修改时间不能为空", trigger: "blur" }
    ],
    isPlatform: [
      { required: true, message: "是否为平台轮播图不能为空", trigger: "change" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询轮播图列表 */
function getList() {
  loading.value = true
  listCarousel(queryParams.value).then(response => {
    carouselList.value = response.rows.filter(item => item.isPlatform === 0)
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    carouselId: null,
    carouselName: null,
    carouselType: null,
    carouselLink: null,
    carouselImage: null,
    status: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("carouselRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.carouselId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加轮播图"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _carouselId = row.carouselId || ids.value
  getCarousel(_carouselId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改轮播图"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["carouselRef"].validate(valid => {
    if (valid) {
      if (form.value.carouselId != null) {
        updateCarousel(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCarousel(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _carouselIds = row.carouselId || ids.value
  proxy.$modal.confirm('是否确认删除轮播图编号为"' + _carouselIds + '"的数据项？').then(function() {
    return delCarousel(_carouselIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/carousel/export', {
    ...queryParams.value
  }, `carousel_${new Date().getTime()}.xlsx`)
}

getList()
</script>
